/**
 * Copyright (C) 2011 GreenSoft S.A.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ar.com.greensoft.neosystem.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 
 * @author Jorge E. Villaverde
 * @since 22/07/2012
 * @version 1.0.0
 */
public class SelectableEntity<T> implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5693741730738514877L;
	private boolean selected = false;
	private T entity;
	
	public SelectableEntity(){
		super();
	}

	public SelectableEntity(T entity){
		super();
		this.entity = entity;
	}
	
	public boolean isSelected() {
		return selected;
	}

	public void setSelected(boolean selected) {
		this.selected = selected;
	}

	public T getEntity() {
		return entity;
	}

	public void setEntity(T entity) {
		this.entity = entity;
	}
	
	public static <T> List<SelectableEntity<T>> createSelectableEntryList(List<T> list){
		List<SelectableEntity<T>> ret = new ArrayList<SelectableEntity<T>>();
		for(T item : list)
			ret.add(new SelectableEntity<T>(item));
		return ret; 
	}

	public static <T> List<T> createSelectedEntityList(List<SelectableEntity<T>> list) {
		List<T> ret = new ArrayList<T>();
		for(SelectableEntity<T> item : list)
			if(item.isSelected())
				ret.add(item.getEntity());
		return ret;
	}

	public static <T> List<SelectableEntity<T>> createSelectableEntryList(Set<T> set) {
		List<SelectableEntity<T>> ret = new ArrayList<SelectableEntity<T>>();
		for(T item : set)
			ret.add(new SelectableEntity<T>(item));
		return ret; 
	}
	
	public static <T> List<SelectableEntity<T>> asSelectedEntityList(List<T> list){
		if(list == null)
			return null;
		List<SelectableEntity<T>> ret = new ArrayList<SelectableEntity<T>>();
		for(T item : list)
			ret.add(new SelectableEntity<T>(item));
		return ret;
	}
	
	public static <T> List<T> asEntityList(List<SelectableEntity<T>> selectedEntity){
		if(selectedEntity == null)
			return null;
		List<T> ret = new ArrayList<T>();
		for(SelectableEntity<T> selItem : selectedEntity){
			if(selItem.isSelected())
				ret.add(selItem.getEntity());
		}			
		return ret;
	}
	
}
